package Day11_2;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortDemo {
    static class CustomComparator implements Comparator<String> {
        private int transfer(String s) {
            switch (s) {
                case "零": return 0;
                case "一": return 1;
                case "二": return 2;
                case "三": return 3;
                case "四": return 4;
                case "五": return 5;
                case "六": return 6;
                case "七": return 7;
                case "八": return 8;
                case "九": return 9;
                default: return -1;
            }
        }

        @Override
        public int compare(String o1, String o2) {
            // o1 和 o2 一定是 "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"
            return transfer(o1) - transfer(o2);
        }
    }

    public static void main1(String[] args) {
        List<String> list = Arrays.asList(
                "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"
        );

        // 使用洗牌算法，先把顺序打乱
        Collections.shuffle(list);

        System.out.println(list);

        // 对这个 list 进行排序
//        Collections.sort(list); // 可以排序，会不会符合预期，不知道，因为是按照 自然顺序 在排序
        // 定义一个比较器来处理

        CustomComparator c = new CustomComparator();

        //Collections.sort(list);传入链表但是需要元素有自然顺序，且符合语义
        //Collections.sort(list, c);//传入链表，和对应比较器
        list.sort(c);//传入比较器

        System.out.println(list);
    }

    static class StudentComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.no - o2.no;
        }
    }

    public static void main(String[] args) {
        Student[] array = {
                new Student(9),
                new Student(5),
                new Student(2),
                new Student(3),
                new Student(1),
                new Student(4),
                new Student(7),
                new Student(0),
                new Student(6),
                new Student(8)
        };  // Student 暂时没有自然顺序，一定抛异常

//      Arrays.sort(array); // 要求元素类型具备自然顺序 && 自然顺序符合预期

        StudentComparator c = new StudentComparator();

        Arrays.sort(array, c);//传入数组，比较器

        System.out.println(Arrays.toString(array));
    }
}
